import { request } from '../utils'
import { format } from 'date-fns'
import { Toast } from "vant"

/*
  获取商品详情
*/
const getProduct = (id, cb) => {
  request({
    url: 'api/Product/GetProductDetail',
    params: {
      id,
      userkey: localStorage.getItem('userKey')
    }
  }).then(res => {
    if(res.data.Success){
      let productData = res.data.Data
      cb && cb(productData)
    } else {
      console.log("获取商品错误...")
    }
  })
}

/*
  获取商品skuId
*/
const getProductSkuId = (requestData, cb) => {
  request({
    url: "api/product/GetSKUInfo",
    params: requestData
  }).then(res => {
    if (res.data.Success) {
      let skuInfo = res.data.Data
      cb && cb(skuInfo)
    } else {
      console.log("获取商品skuInfo失败...")
      Toast(res.data.Msg)
    }
  })
}

/*
  添加商品到购物车
*/
const addProductToCart = (requestData, cb) => {
  request({
    method: 'POST',
    url: "api/cart/PostAddProductToCart",
    data: requestData
  }).then(res => {
    if (res.data.Success) {
      cb && cb()
    } else {
      console.log("添加商品到购物车失败...")
      Toast(res.data.Msg)
    }
  })
}

/*
  收藏商品
*/
const collectProduct = (requestData, cb) => {
  request({
    method: "POST",
    url: "api/product/PostAddFavoriteProduct",
    data: requestData
  }).then((res) => {
    if (res.data.Success) {
      let Msg = res.data.Msg
      cb && cb(Msg)
    } else {
      console.log("收藏商品失败...")
      Toast(res.data.Msg)
    }
  })
}

/*
  获取商品评论
*/
const getProductComments = (requestData, cb) => {
  request({
    url: "api/product/GetProductComment",
    params: requestData
  }).then(res => {
    if (res.data.Success) {
      let list = res.data.Data.List
      cb && cb(list)
    } else {
      console.log("获取商品评论失败...")
      Toast(res.data.Msg)
    }
  })
}

// 获取拼团商品详情
const getPinDetail = (requestData, cb) => {
  request({
    url: "api/FightGroup/GetActiveDetail",
    params: requestData,
  }).then(res => {
    if (res.data.Success) {
      let data = res.data.Data
      data.FightGroupData.MiniGroupPrice = data.FightGroupData.MiniGroupPrice.toFixed(2)
      // data.FightGroupData.EndTime = new Date(data.FightGroupData.EndTime).format('YYYY.MM.DD HH:mm')
      cb && cb(data)
    } else {
      console.log("获取拼团商品失败...")
    }
  })
}

// 订单评价
const orderComment = (requestData, cb) => {
  request({
    method: "POST",
    url: "api/Comment/PostAddComment",
    data: requestData,
  }).then((res) => {
    if (res.data.success) {
      cb && cb()
    } else {
      console.log("评论失败...")
      Toast(res.data.Msg)
    }
  })
}

export {
  getProduct,
  getProductSkuId,
  addProductToCart,
  getProductComments,
  collectProduct,
  getPinDetail,
  orderComment,
}